﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ValuationResource;
using ValuationLogic;

namespace ValuationUI
{
    /// <summary>
    /// Interaction logic for LoginForm.xaml
    /// </summary>
    public partial class LoginForm : Window
    {
        public static string userOnlineID = null;
        MainWindow theMain = null;

        //MainWindow theMain = null;

        public LoginForm()
        {
            InitializeComponent();
            //theMain = new MainWindow(this);
        }

        private void loginBtn_Click(object sender, RoutedEventArgs e)
        {
            if (this.userNameTextBox.Text.Trim().Length == 0)
            {
                MessageBox.Show(this, "User name can't be null");
                return;
            }
            if (this.passwordBox.Password.Trim().Length == 0)
            {
                MessageBox.Show(this, "Password can't be null");
                return;
            }
            User userOnline = new User(this.userNameTextBox.Text, this.passwordBox.Password);
            #region 判断用户是否存在,如果存在
            if (Logic.checkUser(userOnline))
            {
                //new MainWindow().Show();
                theMain = new MainWindow();
                theMain.Show();
                theMain.Owner = this;
                theMain.riskView.Owner = theMain;
                this.userNameTextBox.Clear();
                this.passwordBox.Clear();
                this.Hide();
            }
            else
            {
                MessageBox.Show(this, "User doesn't exist!");
                return;
            }
            //this.Close();
            #endregion
        }

        private void registerBtn_Click(object sender, RoutedEventArgs e)
        {
            RegisterForm newResiterForm = new RegisterForm();
            newResiterForm.Show();
            this.Close();
        }

        private void cancelBtn_Click(object sender, RoutedEventArgs e)
        {
            if (theMain != null)
            {
                theMain.Close();
            }
            this.Close();
        }
    }
}
